package websimulator;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import readwrite.Connector;
import calculations.LevelOfIllumination;
import calculations.Moduls;
import calculations.Temperatur;

/**
 * Servlet implementation class SimulationModulList
 */
@WebServlet("/SimulationModulList")
public class SimulationModulList extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ArrayList<Moduls> modul;
    private Connector con;  
    private Temperatur temp;
	private LevelOfIllumination levelOfIllumination;
    /**
     * @throws Exception 
     * @see HttpServlet#HttpServlet()
     */
    public SimulationModulList() throws Exception {
        super();
        con = new Connector();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String answer="";
		int bigSimId = 0;
		try {
			if(request.getParameter("simId") != "")
				bigSimId = Integer.parseInt(request.getParameter("simId"));
			if(bigSimId != 0)
			{
				modul = con.getBigSimulationModuls(bigSimId);
				answer = "<div id=\"modulList\">";
				answer += "<ul>";
				answer += "<h3>List of Moduls in simulation</h3>";
				answer += "<li>Name</li>";
				for(int i = 0; i < modul.size(); i++)
					answer+= "<li>"+ modul.get(i).getName() +"</li>";
				answer +="</ul>";
				answer +="<input type=\"button\" value=\"update list\" onclick=\"updateList()\">";
				answer += "</div>";
			}
			else
			{
				answer = "<div id=\"modulList\"><h3>List of Moduls in simulation</h3></div>";
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		response.setContentType("text/plain");
		response.getWriter().write(answer);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int no;
		int[] number = null;
		try {
			 modul = con.getAllModuls();
			 number = new int[modul.size()];
			 
			 for(int i = 0; i < modul.size(); i++)
			 {
				 number[i] = con.numberOfHousesInModul(modul.get(i).getId());
			 }
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String answer="";
		String id="";
		 answer += "<div id=\"simModulList\">";
		answer +=		"<table> <tr> <td>Name: </td><td>Number of houses</td><td>Add:</td> </tr>";
		for(int i = 0; i < modul.size(); i++){
			if(number[i] != 0)
			{
				id="modulId"+i;
				try {
					answer +=	"<tr> <td> "+modul.get(i).getName()+" </td> <td> "+number[i]+" </td> <td><input type=\"button\" name=\"add\" id=\"add\" value=\"add\" onclick=\"add('"+i+"');updateList()\"></td></tr>";
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				answer += "<input type=\"hidden\" name=\"id\" id=\""+id+"\" value=\""+modul.get(i).getId()+"\">";
			}
		}
		answer += "</table></div>";
		response.setContentType("text/plain");
		response.getWriter().write(answer);
	}

}
